package com.ctgu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ctgu.common.ResponseResult;
import com.ctgu.model.entity.*;
import com.ctgu.mapper.AdminMapper;
import com.ctgu.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author yiMa
 * @since 2023-07-05
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private MailServiceImpl mailService;
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IRecruitService recruitService;

    @Autowired
    private IExperienceService experienceService;

    @Autowired
    private IEvaluateService evaluateService;

    @Autowired
    private IFilesService filesService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IJobWantedService jobWantedService;

    @Autowired
    private IPositionService positionService;

    @Autowired
    private ConsumerImpl consumer;
    @Autowired
    private IDepartmentService departmentService;
    @Override
    public ResponseResult updateCompanyStatus(Integer id, String status) {
        Company byId = companyService.getById(id);
        if(status.equals("0")){
            rabbitTemplate.convertAndSend("ctguMailExchange","user.email",byId.getEmail()+"0");
        }
        else
            rabbitTemplate.convertAndSend("ctguMailExchange","user.email",byId.getEmail()+"1");

        if(Objects.isNull(byId)){
            return new ResponseResult<>(400,"公司id不存在");
        }
        byId.setStatus(status);
        companyService.updateById(byId);
        return new ResponseResult(200,"更新成功");
    }

    @Override
    public ResponseResult updateEmployeeStatus(Integer id, String status) {
        Employee byId = employeeService.getById(id);;
        if(Objects.isNull(byId)){
            return new ResponseResult<>(400,"人才id不存在");
        }
        byId.setStatus(status);
        employeeService.updateById(byId);
        return new ResponseResult(200,"更新成功");
    }

    @Override
    @Transactional
    public ResponseResult deleteCompany(Integer id) {
        Company byId = companyService.getById(id);
        if(Objects.isNull(byId)){
            return new ResponseResult<>(400,"公司id不存在");
        }
        //删除公司后也要删除相应招聘信息
        LambdaQueryWrapper<Recruit> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Recruit::getCompanyName,byId.getCompanyName());
        recruitService.remove(wrapper);
        //还要把该公司所有员工改成离职
        LambdaQueryWrapper<Employee> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.eq(Employee::getCompanyId,byId.getId());
        List<Employee> list = employeeService.list(wrapper1);
        list=list.stream().map(item->{
            item.setCompanyId(-1);
            item.setBeginDate(null);
            item.setPosId(-1);
            item.setDepId(-1);
            item.setWorkState("离职");
            return item;
        }).collect(Collectors.toList());
        List<Integer> integerList=list.stream().map(item->item.getId()).collect(Collectors.toList());
        LambdaUpdateWrapper<Experience> wrapper4=new LambdaUpdateWrapper<>();
        wrapper4.in(Experience::getEId,integerList);
        wrapper4.set(Experience::getTerminationDate,LocalDateTime.now());
        experienceService.update(wrapper4);

        employeeService.updateBatchById(list);
        //删除公司后也要删除部门信息
        LambdaQueryWrapper<Department> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(Department::getCompanyId,byId.getId());
        //删除职位
        List<Department> list1 = departmentService.list(wrapper2);
        List<Integer> integers=list1.stream().map(item->{
            return item.getId();
        }).collect(Collectors.toList());
        LambdaQueryWrapper<Position> wrapper3=new LambdaQueryWrapper<>();
        wrapper3.in(Position::getDepId,integers);
        positionService.remove(wrapper3);
        departmentService.remove(wrapper2);
        companyService.removeById(id);
        return new ResponseResult<>(200,"删除成功");
    }

    @Override
    @Transactional
    public ResponseResult deleteEmployee(Integer[] ids) {
        List<Integer> integers=Arrays.asList(ids);
        //删除员工后也要删除相应招聘信息
        LambdaQueryWrapper<Recruit> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(Recruit::getEId,integers);
        recruitService.remove(wrapper);
        //删除工作经历
        LambdaQueryWrapper<Experience> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.in(Experience::getEId,integers);
        experienceService.remove(wrapper1);
        //删除评价表
        LambdaQueryWrapper<Evaluate> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(Evaluate::getE_id,integers);
        evaluateService.remove(wrapper2);
        //删除人才档案表
        LambdaQueryWrapper<Files> wrapper3=new LambdaQueryWrapper<>();
        wrapper3.eq(Files::getEId,integers);
        filesService.remove(wrapper3);
        //删除求职表
        LambdaQueryWrapper<JobWanted> wrapper4=new LambdaQueryWrapper<>();
        wrapper4.eq(JobWanted::getEId,integers);
        jobWantedService.remove(wrapper4);
        employeeService.removeByIds(integers);
        return new ResponseResult<>(200,"删除成功");
    }

    //更新超管个人信息
    public ResponseResult updateAdminInfo(Admin admin){
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int id=loginUser.getUser().getId();
        Admin admin1 = getById(id);
        final CopyOptions copyOptions = CopyOptions.create();
        copyOptions.setOverride(false);
        copyOptions.setIgnoreNullValue(true);
        BeanUtil.copyProperties(admin1,admin, copyOptions);
        updateById(admin);
        return new ResponseResult<>(200,"更新成功");
    }
}
